<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //1.复杂解构
        // let wangfei = {
        //     name: '王菲',
        //     age: 18,
        //     songs: ['红豆', '流年', '暧昧', '传奇'],
        //     history: [
        //         { name: '窦唯' },
        //         { name: '李亚鹏' },
        //         { name: '谢霆锋' }
        //     ]
        // };
        // console.log(userName,age,one,two,three,four,name,name2,name3);
        // 王菲 18 红豆 流年 暧昧 传奇 窦唯 李亚鹏 谢霆锋

        let {userName,age,songs:[one,two,three,four],history:[{name},{name2},{name3}]} = {
            userName: '王菲',
            age: 18,
            songs: ['红豆', '流年', '暧昧', '传奇'],
            history: [
                { name: '窦唯' },
                { name2: '李亚鹏' },
                { name3: '谢霆锋' }
            ]
        };
        console.log(userName,age,one,two,three,four,name,name2,name3);
       


        // 2.defineProperties-读写器求和及求平均数 
        var obj = {
            userscore:[
                {
                    username:"老王",
                    score:60
                },
                {
                    username:"老李",
                    score:70
                },
                {
                    username:"老高",
                    score:80
                },
                {
                    username:"老洪",
                    score:90
                }
            ],
        };
        Object.defineProperties(obj,{
            // 读写器求和
            num:{
                get:function(){
                    let scoarr=this.userscore;
                    // console.log(scoarr);
                    var num=0;
                    for(let i=0;i<scoarr.length;i++){
                        num+=scoarr[i].score;
                    }
                    return num;
                    
                }
            },
            // 求平均数
            average:{
                get:function(){
                    let scoarr=this.userscore;
                     average=this.num/scoarr.length;
                     return average;
                }
            }
        })
        console.log(obj.num);
        console.log(obj.average);
    </script>
</body>

</html>